#include <QtGui>
#include <QtDebug>

#include "bubble.h"


/**********************
 * Constructor
 */
Bubble::Bubble(int x, int y, bool child)
{
    origin = (qrand() % ((2*x + 1))) - x;
    size = 1 + qrand() % 3;
    radius = size * 20;
    max = qrand() % (x/5);
    location[0] = origin;
    location[1] = y;
    speed = 1 + (qrand() % 3);
    int lor = qrand() % 2;
    if(lor = 0) lr = true;
    else lr = false;
    special = 0;

    if(!child && (qrand() % 100) > 70){
        theSpecialOne();
    }  else {
        innerColor = randomColor();
        outerColor = randomColor();
        updateBrush();
    }

}

/**********************
 * Make special bubbles
 */
void Bubble::theSpecialOne(){
    special = 1 + (qrand() % 3);
    size = 0;
    radius = 15;
    if(special == 3) innerColor =  QColor(0, 255, 0, 120);
    else innerColor = (special == 1 ? QColor(255, 0, 0, 120) :  QColor(0, 0, 255, 120));
    outerColor = QColor(0, 0, 0, 190);
    updateBrush();
}


/**********************
 * Used for setting location of child bubbles
 */
void Bubble::setSize(int x, int y, int sz){
    origin = x;
    size = sz;
    radius = size * 20;
    location[0] = origin;
    location[1] = y;
}


/**********************
 * Destructor
 */
Bubble::~Bubble(){
}


/**********************
 * Set brush settings
 */
void Bubble::updateBrush()
{
    QRadialGradient gradient(QPointF(radius, radius), radius,
                             QPointF(radius*0.5, radius*0.5));

    gradient.setColorAt(0, QColor(255, 255, 255, 255));
    gradient.setColorAt(0.25, innerColor);
    gradient.setColorAt(1, outerColor);
    brush = QBrush(gradient);
}


/**********************
 * ..... yeah
 */
void Bubble::drawBubble(QPainter *painter)
{
    painter->save();
    painter->translate(location[0] - radius, location[1] - radius);
    painter->setBrush(brush);
    painter->drawEllipse(0, 0, int(2*radius), int(2*radius));
    painter->restore();
}


/**********************
 * Create a color
 */
QColor Bubble::randomColor()
{
    int red = int(205 + 50.0*qrand()/(RAND_MAX+1.0));
    int green = int(205 + 50.0*qrand()/(RAND_MAX+1.0));
    int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0));
    int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0));

    return QColor(red, green, blue, alpha);
}


/**********************
 * Handle movement
 */
void Bubble::move()
{
    location[1] -= speed;
    if(lr) location[0] ++;
    else location[0] --;
    int diff  = (location[0] - origin);
    if((diff*diff)/2 > max) lr = !lr;
}


/**********************
 * .... yeah
 */
int Bubble::getRadius()
{
    return radius;
}

